package org.meichao.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {

    /**
     * 注册一个StatViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet(){
        ServletRegistrationBean srb = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //添加初始化参数
        //白名单：没有配置或者为空，则允许访问
        srb.addInitParameter("allow","127.0.0.1");
        //黑名单:(共同存在时,deny优先allow)提示：Sorry,you are not permitted to view this page.
        //srb.addInitParameter("deny","");
        srb.addInitParameter("loginUsername","admin");
        srb.addInitParameter("loginPassword","admin");
        //是否能够重置数据源，禁用HTML页面上的"Reset ALL"功能
        srb.addInitParameter("resetEnable","false");
        return srb;
    }

    /**
     * 注册一个filterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean druidStatFilter(){
        FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
        frb.addUrlPatterns("/*");
        //添加要忽略的格式信息
        frb.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return frb;
    }
}
